﻿using UnityEngine;
using System.Collections;
using Vectrosity;

public class CVectrocityLine
{
	
	public VectorLine myLine;
	public Vector3[] mylinePoint;
	public Color[] lineColors;
	public int lineIndex = 0;
	vectrocityLine myParent;
	bool pointEnded;
	int maxPoints;
	float initialTime;
	
	public CVectrocityLine(int myMaxPoints,Material lineMaterial,float width, LineType lineType, Joins joins, vectrocityLine parent)
	{
		pointEnded = false;
		maxPoints = myMaxPoints;
		mylinePoint = new Vector3[maxPoints];
		lineColors = new Color[maxPoints - 1];
		myParent = parent;
		lineIndex = 0;
		myLine = new VectorLine("DrawnLine", mylinePoint, lineMaterial, width, lineType, joins);
		myLine.minDrawIndex = 0;
		myLine.maxDrawIndex = 0;
	}
	
	public void Update()
	{
		bool stillCanSee = false;
		for(int i = 0; i < lineIndex; i++)
		{
			if(i < maxPoints)
			{
				if(lineColors[i].a > 0)
				{
					stillCanSee = true;
					lineColors[i].a -= Time.deltaTime * 1.5f;
				}
				else
				{
					lineColors[i].a = 0;
					myLine.minDrawIndex = i;					
				}
			}
		}
		
		myLine.SetColorsSmooth(lineColors);
		if(pointEnded && !stillCanSee)
		{
			VectorLine.Destroy(ref myLine);
			myParent.listOfLines.Remove(this);
			return;
		}
		//if(Input.GetKeyDown(KeyCode.Space))
		//	Application.LoadLevel("GameScene(Proto)");
	}
	
	public void addPoint(Vector3 p_Point)
	{
		mylinePoint[lineIndex] = p_Point;
		//Debug.Log("THATS IDX :" + lineIndex);
		if(lineIndex < maxPoints - 1)
		{
			lineColors[lineIndex + 1] = Color.white;
			lineColors[lineIndex + 1].a += 10;
		}
		myLine.maxDrawIndex = lineIndex;
		lineIndex ++;
		myLine.Draw3DAuto();
	}
	
	public void endPoint()
	{
		pointEnded = true;
	}
	
	/*
	 * public Vector3 point;
	public float time;
	public bool isEndPt;
	
	public CVectrocityLine()
	{
		point = Vector3.zero;
		time = 0;
		isEndPt = false;
	}
	
	public CVectrocityLine(Vector3 _point, float _time)
	{
		point = _point;
		time = _time;
		isEndPt = false;
	}
	
	public CVectrocityLine(Vector3 _point, float _time, bool _isEnd)
	{
		point = _point;
		time = _time;
		isEndPt = _isEnd;
	}
	*/
}
